package com.kawaiim.zhj.Operator;

import android.graphics.Color;

import java.util.Collections;
import java.util.List;
import java.util.Random;

import com.kawaiim.zhj.OutCard.OutCardStyle;
import com.kawaiim.zhj.OutCard.OutCardStyleEnum;
import com.kawaiim.zhj.entity.Card;
import com.kawaiim.zhj.entity.CardOrderMode;
import com.kawaiim.zhj.entity.Player;
import com.kawaiim.zhj.utilities.Constants;

public class LordGameMode {

    //将卡的编号转化为Card类型
    public static Card ConvertCard(int cardIndex) {

        //把A,2变为14,15，
        int size = (cardIndex / Constants.PLAYERS_SIZE + 1);

        //如果是1和2，加13；如果是大王小王，再加2
        if (size <= 2)  {
            size += 13;
        }

        //返回卡牌的类型
        return new Card(size, cardIndex % Constants.PLAYERS_SIZE, cardIndex, 0);
    }

    //发牌的流程
    public void Shuffle(Player [] pl) {
        int[] num = new int[Constants.MAX_CARD_NUM];

        //生成52张牌
        for (int i = 0; i < Constants.MAX_CARD_NUM; i++) {
            num[i] = i ;
        }

        //生成随机种子
        Random random = new Random();

        //随机打乱牌序(Fisher-Yates算法)
        for (int i = Constants.MAX_CARD_NUM-1; i >= 0; i--) {
            int randNum = random.nextInt(i+1);
            int temp = num[i];
            num[i] = num[randNum];
            num[randNum] = temp;
          
        }

        //发牌
        for (int i = 0; i < Constants.MAX_CARD_NUM; i++) {
            Card card = ConvertCard(num[i]);
                
                 //todo
                //对应人的队列增加相应卡牌
                pl[i %Constants.PLAYERS_SIZE].cards.add(card);
                if (card.cardSize == 3 && card.color == 1)
                {
                    pl[i % Constants.PLAYERS_SIZE].isRedThree =true;
                }
                if (card.cardSize == 14 && (card.color == 1 || card.color ==3 ))
                {
                    pl[i % Constants.PLAYERS_SIZE].isRedA = true;
                    pl[i % Constants.PLAYERS_SIZE].setName("红尖儿",Color.RED);
                }

        }

    }

    //改变牌的顺序，0是让牌按牌的大小排，1是让牌按的数量排
    public static void changeOrder(List<Card> cards, CardOrderMode orderMode) {
        switch (orderMode) {
            case CARD_NUM:
                Collections.sort(cards, Card.CompareModeCardNum);
                break;
            case CARD_SIZE:
                Collections.sort(cards, Card.CompareModeCardSize);
                break;
            case CARD_DISPLAY:
                Collections.sort(cards, Card.CompareModeDisplayCard);
                break;
        }
    }

    //判断出牌是否合法
    public boolean canOutCard(List<Card> prevCard, List<Card> myCard) {
        //获取自家的CardStyle
        OutCardStyle myOutCardStyle = OutCardStyle.judgeCardStyle(myCard);

        //如果自家出牌不合法，返回false
        if (myOutCardStyle.outCardStyleEnum == OutCardStyleEnum.CANT_OUT) {
            return false;
        }

        //如果上次出牌的是自己，且自家牌合法，则可以打出
        if (prevCard == null || prevCard.size() == 0) {
            return true;
        }

        //获取上家的cardStyle
        OutCardStyle preOutCardStyle = OutCardStyle.judgeCardStyle(prevCard);


        //上家出炸弹
        if (preOutCardStyle.outCardStyleEnum == OutCardStyleEnum.BOMB) {
            //必须自家也是炸弹，比它长，或者相同长度比它大
            return myOutCardStyle.outCardStyleEnum == OutCardStyleEnum.BOMB
                    || preOutCardStyle.cardLength < myOutCardStyle.cardLength
                    || preOutCardStyle.cardLength == myOutCardStyle.cardLength
                    && preOutCardStyle.firstCardSize < myOutCardStyle.firstCardSize;

        }
        //如果上家出其他类型的牌（自家能出炸弹，或者是必须为相同类型，相同长度且要比它大
        else {
            return myOutCardStyle.outCardStyleEnum == OutCardStyleEnum.BOMB
                    || preOutCardStyle.outCardStyleEnum == myOutCardStyle.outCardStyleEnum
                    && preOutCardStyle.cardLength == myOutCardStyle.cardLength
                    && preOutCardStyle.firstCardSize < myOutCardStyle.firstCardSize;
        }
    }

}
